﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using Lizk.SimpleHUD;

namespace SharpMedia
{

    [XmlRoot]
    public class MainMenuItem
    {
        private string name;
        private string image;

        public string Image
        {
            get { return image; }
            set { image = value; }
        }
        private string selectImage;

        public string SelectedImage
        {
            get { return selectImage; }
            set { selectImage = value; }
        }

        private MenuAction action;

        public MenuAction Action
        {
            get { return action; }
            set { action = value; }
        }

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public string Type { get; set; }

        public MainMenuItem() { }
        public MainMenuItem(MenuAction action, string image, string name, string selectImage)
        {
            this.action = action; this.image = image; this.name = name; this.selectImage = selectImage;
        }

        public enum MenuAction : int
        {
            ListType = 0,
            FileBrowser = 3,
            DVDs = 4,
            TvDevice = 5,
            Newest = 6
        }

    }

    class MainMenuItemControl : HUDContainerSimple
    {
        public MainMenuItem Item { get; set; }

        public MainMenuSubList SubList { get; set; }
        
        public MainMenuItemControl(MainMenuItem item, SizeS itemSize)
        {
            this.Item = item;
            Bounds = new RectangleS(PointS.Empty, itemSize);

            HUDImage selection = new HUDImage(item.SelectedImage);
            selection.MovementSpeed = 8f;
            selection.Size = itemSize;
            selection.Alpha = 0;
            selection.FadeOnLoad = true;
            Add(selection);

            img = new HUDImage(item.Image);
            img.Bounds = new RectangleS(10,10, 140, 140);
            img.FadeOnLoad = true;
            img.Alpha = 127;
            //img.OnMouseEnter += new EventHandler<MouseEventArgs>(img_OnMouseEnter);
            //img.OnMouseExit += new EventHandler<MouseEventArgs>(img_OnMouseExit);
            img.OnMouseDown += new EventHandler<MouseEventArgs>(cont_OnMouseUp);
            Add(img);

        }
        HUDImage img;
        public override void Update(TimeSpan ts)
        {
            base.Update(ts);

            if (!SharpMedia.Engine.IsCursorHidden && this.IsMouseOver)
            {
                img.Fade(255, 6f);
            }
            else
            {
                if (img.Parent == ((HUDMoveList)img.Parent.Parent).SelectedControl)
                    img.Fade(255, 6f);
                else
                    img.Fade(127, 6f);
            }

        }

        void cont_OnMouseUp(object sender, HUDControl.MouseEventArgs e)
        {
            HUDImage cont = (HUDImage)sender;

            if (e.Button == MouseButtons.Left)
            {
                int index = ((HUDMoveList)cont.Parent.Parent).IndexOf(cont.Parent);
                if (index >= 0)
                {
                    ((HUDMoveList)cont.Parent.Parent).SelectedIndex = index;
                }
            }
        }
    }

    class MainMenuSubListItem : HUDContainerSimple
    {
        HUDMoveList list; 
        
        HUDImage selection;
        public MainMenuSubListItem(HUDMoveList list, SizeS itemSize, string img, string name, string days)
        {
            this.list = list;
            Location = new PointS(0, 0);
            Size = itemSize;
            //OnMouseUp += new EventHandler<MouseEventArgs>(item_OnMouseUp);

            selection = new HUDImage("selection96.png");
            selection.Size = itemSize;
            selection.ConstrainProportions = false;
            selection.Alpha = 0;
            Add(selection);

            HUDImage image = new HUDImage(img);
            image.Location = new PointS(3, 3);
            image.Size = new SizeS(90, 90);
            image.FadeOnLoad = true;
            Add(image);

            HUDLabel label = new HUDLabel(name, "tahoma25b");
            label.Bounds = new RectangleS(98, 25, itemSize.Width - 98, 40);
            label.Alignment = Alignment.Top;
            label.WordBreak = false;
            Add(label);

            HUDLabel label4 = new HUDLabel(days, "tahoma14b");
            label4.Bounds = new RectangleS(110, 50, itemSize.Width - 102, 20);
            label4.Alignment = Alignment.Top;
            label4.WordBreak = false;
            Add(label4);
        }

        public override void Update(TimeSpan ts)
        {
            base.Update(ts);
            if (SharpMedia.Engine.IsCursorHidden || !this.IsMouseOver)
            {
                selection.Fade(0, 6f);
            }
            else if (!SharpMedia.Engine.IsCursorHidden && this.IsMouseOver)
            {
                HUDMoveList sublist = (HUDMoveList)this.Parent;

                int listindex = ((HUDContainerSimple)sublist.Parent.Parent).IndexOf(sublist.Parent);
                if (listindex != list.SelectedIndex)
                {
                    selection.Fade(0, 6f);
                    return;
                }
                selection.Fade(100, 6f);
            }
        }


    }

    class MainMenuSubList : HUDContainerSimple
    {
        public HUDAlphaList List { get; private set; }
        public HUDImage SelectionImage { get; private set; }
        public MainMenuSubList()
        {
            Alpha = 0;


            SelectionImage = new HUDImage("selection96.png")
            {
                ConstrainProportions = false,
                Color = ColorS.Black
            };
            Add(SelectionImage);

            List = new HUDAlphaList(new SizeS(400, 96), false);
            List.Anchor = Anchor.All;
            Add(List);

            List.SelectedIndexChanged += new EventHandler<SelectionEventArgs>(delegate(object sender, SelectionEventArgs args) { ((HUDControl)sender).Parent.Get<HUDImage>(0).Destination = ((HUDMoveList)sender).Selection.Location; });
        }
    }
}
